using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BISPOS.Repository;
using System.Data.Linq;

namespace BISPOS.Report
{
    public class SoldProductGroup
    {
        private BISPOSDataClassesDataContext db = new BISPOSDataClassesDataContext();
        private DateTime fromDate;
        private DateTime toDate;

        private int id;
        private string name;
        private string code;

        public bool IsSold
        {
            get { return (this.SoldProducts.Count() != 0); }
        }
        public SoldProductGroup(DateTime _fromDate, DateTime _toDate, int _id)
        {
            fromDate = _fromDate;
            toDate = _toDate;
            id = _id;

            ProductGroup productGroup = db.ProductGroups.FirstOrDefault(x => x.Id == id);
            name = productGroup.Name;
            code = productGroup.Code;
        }

        public System.Data.Linq.EntitySet<SoldProduct> SoldProducts
        {
            get
            {
                EntitySet<SoldProduct> soldProducts = new EntitySet<SoldProduct>();
                foreach (var invoice in db.Invoices.Where(x =>
                     ((x.Date.Date >= fromDate.Date) && (x.Date.Date <= toDate.Date))))
                {
                    foreach (var item in invoice.InvoiceItems.Where(x => x.Stock.Product.ProductGroupId == this.id))
                    {
                        if (soldProducts.Any(x => x.Name == item.ProductName))
                        {
                            SoldProduct product = soldProducts.Where(x => x.Name == item.ProductName).FirstOrDefault();
                            string productName = product.Name;
                            decimal amount = product.SoldAmount;
                            double quantity = product.SoldQuantity;
                            soldProducts.Remove(product);
                            soldProducts.Add(new SoldProduct
                            {
                                Name = productName,
                                SoldQuantity = quantity + item.Quantity,
                                SoldAmount = amount + item.TotalPrice,
                                BarCode = product.BarCode
                            });
                        }
                        else
                        {
                            soldProducts.Add(new SoldProduct
                            {
                                Name = item.ProductName,
                                SoldAmount = item.TotalPrice,
                                SoldQuantity = item.Quantity,
                                BarCode = item.Stock.Product.Barcode
                            });
                        }
                    }
                }
                return soldProducts;
            }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public string Code
        {
            get { return code; }
            set { code = value; }
        }

    }
}
